package com.example.dynamicschedule.task;

import com.alibaba.fastjson.JSON;
import com.example.dynamicschedule.bean.*;
import com.example.dynamicschedule.dao.*;
import com.example.dynamicschedule.redis.RedisService;
import com.example.dynamicschedule.utils.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CreditDataRecoveryTask {
    static Logger log = Logger.getLogger(CreditDataRecoveryTask.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private CkCreditCardDownloadNewTonglianMapper ckCreditCardDownloadNewTonglianMapper;
    @Autowired
    private CkCreditCardDownloadOldTonglianMapper ckCreditCardDownloadOldTonglianMapper;
    @Autowired
    private CkCreditCardDownloadNewYinshuMapper ckCreditCardDownloadNewYinshuMapper;
    @Autowired
    private CkCreditCardDownloadOldYinshuMapper ckCreditCardDownloadOldYinshuMapper;
    @Autowired
    private DateUtils dateUtils;

    /**
     * 回收大数据定时任务
     */
    @Transactional
    public void CreditDataRecoveryTask() throws Exception {
        log.info("信贷回收大数据定时任务---------------开始");
        log.info("信贷回收大数据定时任务---------------清空回收大数据表--------------开始");
        ckCreditCardDownloadNewTonglianMapper.delete();
        ckCreditCardDownloadOldTonglianMapper.delete();
        ckCreditCardDownloadNewYinshuMapper.delete();
        ckCreditCardDownloadOldYinshuMapper.delete();
        log.info("信贷回收大数据定时任务---------------清空回收大数据表--------------结束");
        log.info("信贷回收大数据定时任务---------------回收通联数据到大数据--------------开始");
        String nowDayStr = dateUtils.getNowStr();
        String newTongLian ="tjcssys@creditNewTongLian@"+nowDayStr;
        String oldTongLian="tjcssys@creditOldTongLian@"+nowDayStr;
        if (redisService.hasKey(newTongLian)) {
            log.info("信贷回收大数据定时任务---------------回收通联新表数据到大数据--------------开始");
            Object o = redisService.get(newTongLian);
            List<CkCreditCardDownloadNewTonglian> ckCreditCardDownloadNewTonglians = JSON.parseArray(String.valueOf(o), CkCreditCardDownloadNewTonglian.class);
            ckCreditCardDownloadNewTonglianMapper.insertForEach(ckCreditCardDownloadNewTonglians);
            redisService.del(newTongLian);
            ckCreditCardDownloadNewTonglians.clear();
            log.info("信贷回收大数据定时任务---------------回收通联历史表数据到大数据--------------结束");
        }
        if (redisService.hasKey(oldTongLian)) {
            log.info("信贷回收大数据定时任务---------------回收通联新表数据到大数据--------------开始");
            Object o = redisService.get(oldTongLian);
            List<CkCreditCardDownloadOldTonglian> ckCreditCardDownloadOldTonglians = JSON.parseArray(String.valueOf(o), CkCreditCardDownloadOldTonglian.class);
            ckCreditCardDownloadOldTonglianMapper.insertForEach(ckCreditCardDownloadOldTonglians);
            redisService.del(oldTongLian);
            ckCreditCardDownloadOldTonglians.clear();
            log.info("信贷回收大数据定时任务---------------回收通联历史表数据到大数据--------------结束");
        }
        log.info("信贷回收大数据定时任务---------------回收通联数据到大数据--------------结束");
        log.info("信贷回收大数据定时任务---------------回收银数数据到大数据--------------开始");
        String newYinShu ="tjcssys@creditNewYinShu@"+nowDayStr;
        String oldYinShu="tjcssys@creditOldYinShu@"+nowDayStr;
        if (redisService.hasKey(newYinShu)) {
            log.info("信贷回收大数据定时任务---------------回收银数新表数据到大数据--------------开始");
            Object o = redisService.get(newYinShu);
            List<CkCreditCardDownloadNewYinshu> ckCreditCardDownloadNewYinShus = JSON.parseArray(String.valueOf(o), CkCreditCardDownloadNewYinshu.class);
            ckCreditCardDownloadNewYinshuMapper.insertForEach(ckCreditCardDownloadNewYinShus);
            redisService.del(newYinShu);
            ckCreditCardDownloadNewYinShus.clear();
            log.info("信贷回收大数据定时任务---------------回收银数历史表数据到大数据--------------结束");
        }
        if (redisService.hasKey(oldYinShu)) {
            log.info("信贷回收大数据定时任务---------------回收银数新表数据到大数据--------------开始");
            Object o = redisService.get(oldYinShu);
            List<CkCreditCardDownloadOldYinshu> ckCreditCardDownloadOldYinShus = JSON.parseArray(String.valueOf(o), CkCreditCardDownloadOldYinshu.class);
            ckCreditCardDownloadOldYinshuMapper.insertForEach(ckCreditCardDownloadOldYinShus);
            redisService.del(oldYinShu);
            ckCreditCardDownloadOldYinShus.clear();
            log.info("信贷回收大数据定时任务---------------回收银数历史表数据到大数据--------------结束");
        }
        log.info("信贷回收大数据定时任务---------------回收银数数据到大数据--------------结束");
    }
}
